//
//  YBSMSNoticeViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBSMSNoticeViewController.h"
#import "YBSMSWaterLayoutView.h"
#import "PlaceholderTextView.h"
#import "LCustomAlertView.h"
#import "YBPeopleInfoModel.h"



@interface YBSMSNoticeViewController ()<UITextViewDelegate , UITextFieldDelegate , LPChooseYearAndMonthAndDayAndHourAndMinuteDelegate , LCustomAlertDelegate , YBSMSWaterLayoutViewDelegate>
/**  back  */
@property (strong, nonatomic) UIScrollView  *backScrollView;

@property (nonatomic , strong) YBSMSWaterLayoutView * selectView;

@property (nonatomic , strong) PlaceholderTextView * contentTextView;

@property (nonatomic , strong) UIButton * sendButton;

@property (nonatomic , strong) JCAlertView * jcAlertView;

@property (nonatomic , strong) LPChooseYearAndMonthAndDayAndHourAndMinuteView * pickTime;

@property (nonatomic , assign) NSInteger currentDeleteIndex;

@property (nonatomic , strong) NSMutableArray<YBPeopleInfoModel *> * dataArray;
@end

@implementation YBSMSNoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"短信提醒";
    self.currentDeleteIndex = 0;
    [self ybInitUserInterface];
    [self ybInitDataSource];
}


-(void)ybInitUserInterface{
    [self.view addSubview:self.backScrollView];
    [self.backScrollView addSubview:self.selectView];
    
    UIView * bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.backScrollView addSubview:bottomView];
    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], ItemHeight * 20)];
    lineView.backgroundColor = [UIColor themeGrayColor];
    [bottomView addSubview:lineView];
    
    [bottomView addSubview:self.contentTextView];
    [bottomView addSubview:self.sendButton];
    
    
    bottomView.sd_layout.topSpaceToView(self.selectView , 0).leftSpaceToView(self.backScrollView , 0).widthIs([UIScreen zj_screenWith]).heightIs(ItemHeight * 470);
    
    [self.backScrollView setupAutoContentSizeWithBottomView:bottomView bottomMargin:ItemHeight * 50];
    
    self.contentTextView.contentText = [NSString stringWithFormat:@"%@于%@发您的“%@”至今尚未阅读，请尽快登录工信党建进行查阅！如您未下载工信党建App，请点击链接：%@ 进行下载。谢谢！" , ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.F_DEPT_NAME , self.model.MSG_START_TIME , self.model.TITLE , DOWNLOADURL];
    
}





-(void)ybInitDataSource{
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    [YBRequestManager request_unReadMsgPeopleListWithMsgId:self.model.ID successBlock:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray * tempArray = [NSArray yy_modelArrayWithClass:[YBPeopleInfoModel class] json:object[@"MSG_USERS"]];
            self.dataArray = [tempArray mutableCopy];
            self.selectView.dataArray = tempArray;
        }
        else{
            [YJProgressHUD showSuccess:@"获取失败" inview:self.view];
        }
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
    }];
}






-(void)option_selectButton:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.selectView.frame = CGRectMake(0, 0, [UIScreen zj_screenWith], ItemHeight * 480);
        self.selectView.dateInputTextField.hidden = NO;
    }
    else{
        self.selectView.frame = CGRectMake(0, 0, [UIScreen zj_screenWith], ItemHeight * 400);
        self.selectView.dateInputTextField.hidden = YES;
    }
    
}

-(void)option_sendButton:(UIButton *)sender{
    if (self.selectView.selectButton.selected) {
        if (self.selectView.dateInputTextField.text.length == 0) {
            [YJProgressHUD showSuccess:@"请选择发送时间" inview:self.view];
            return;
        }
        if ([[NSDateFormatter dateHourAndMintueWithString:self.selectView.dateInputTextField.text] timeIntervalSinceDate:[NSDate date]] <= 0) {
            [YJProgressHUD showSuccess:@"发送时间必须大于当前时间" inview:self.view];
            return;
        }
    }
    if (self.contentTextView.text.length == 0) {
        [YJProgressHUD showSuccess:@"请输入短信内容" inview:self.view];
    }
    NSString * sendTime = @"";
    if (self.selectView.selectButton.selected) {
        sendTime = [NSString stringWithFormat:@"%@:00" , self.selectView.dateInputTextField.text];
    }
    [YJProgressHUD showMessage:@"发送中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    NSString * peopleIdString = @"";
    for (YBPeopleInfoModel * model in self.dataArray) {
        peopleIdString = [NSString stringWithFormat:@"%@,%@" , peopleIdString , model.ACCOUNT_ID];
    }
    if (peopleIdString.length > 0) {
        peopleIdString = [peopleIdString substringFromIndex:1];
    }
    [YBRequestManager request_sendSMSNoticeWithMsgId:self.model.ID msgContent:self.contentTextView.text sendTime:sendTime peoplesId:peopleIdString successBlock:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            [YJProgressHUD showSuccess:@"发送成功" inview:self.view];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        else{
            [YJProgressHUD showSuccess:@"发送失败" inview:self.view];
        }
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
    }];
}


-(void)YBSMSWaterLayoutViewDidClickDeleteButtonWithIndex:(NSInteger)index{
    self.currentDeleteIndex = index;
    YBPeopleInfoModel * tempModel = self.dataArray[index];
    LCustomAlertView *alert = [[LCustomAlertView alloc] initWithFrame:self.view.bounds alertMessage:[NSString stringWithFormat:@"是否删除 %@-%@" , [ToolClass returnNoNullStringWithString:tempModel.F_DEPT_NAME] , [ToolClass returnNoNullStringWithString:tempModel.NAME]] titlesArray:@[@"取消",@"确定"]];
    alert.delegate = self;
    self.jcAlertView = [[JCAlertView alloc]initWithCustomView:alert dismissWhenTouchedBackground:YES];
    [self.jcAlertView show];
}


#pragma mark - LCustomAlertDelegate
- (void)lCustomAlertWithView:(LCustomAlertView *)view index:(NSInteger)index{
    if (index == 1) {
        /**  确认  */
        [self.dataArray removeObjectAtIndex:self.currentDeleteIndex];
        self.selectView.dataArray = [self.dataArray copy];
    }
    [self.jcAlertView dismissWithCompletion:nil];
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self.contentTextView endEditing:YES];
    [self.pickTime showCurrentTime];
    return NO;
}


- (void)ChooseYearAndMonthAndDayAndHourAndMinuteWithTitle:(NSString * )string{
    //选择年月日返回代理方法
    self.selectView.dateInputTextField.text = string;
}





- (UIScrollView *)backScrollView{
    if (!_backScrollView ) {
        _backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0 , navHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - navHeight)];
        _backScrollView.showsVerticalScrollIndicator = NO;
        _backScrollView.showsHorizontalScrollIndicator = NO;
        _backScrollView.contentSize = _backScrollView.size;
        _backScrollView.backgroundColor = [UIColor whiteColor];
    }
    return _backScrollView;
    
}


-(YBSMSWaterLayoutView *)selectView
{
    if (!_selectView) {
        _selectView = [[YBSMSWaterLayoutView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], ItemHeight * 400) title:@"发布对象(0):"];
        [_selectView.selectButton addTarget:self action:@selector(option_selectButton:) forControlEvents:UIControlEventTouchUpInside];
        _selectView.dateInputTextField.delegate = self;
        _selectView.delegate = self;
    }
    return _selectView;
}


- (PlaceholderTextView *)contentTextView{
    if (!_contentTextView) {
        _contentTextView = [[PlaceholderTextView alloc]initWithFrame:CGRectMake(ItemWidth * 30, ItemHeight * 70, [UIScreen zj_screenWith] - ItemWidth * 60, ItemHeight * 270)];
        _contentTextView.backgroundColor = [UIColor themeGrayColor];
        _contentTextView.placeholder = @"不得输入任何涉密内容";
        _contentTextView.clipsToBounds = YES;
        _contentTextView.layer.cornerRadius = normalCornerRadius;
        _contentTextView.returnKeyType = UIReturnKeyDone;
        _contentTextView.font = LYPFONT(16);
    }
    return _contentTextView;
}


-(UIButton *)sendButton
{
    if (!_sendButton) {
        _sendButton = [ToolClass createButtonWithFrame:CGRectMake(ItemWidth * 100, ItemHeight * 370, [UIScreen zj_screenWith] - ItemWidth * 200, ItemHeight * 100) text:@"发送短信" textColor:[UIColor whiteColor] imageName:@"" font:16];
        _sendButton.backgroundColor = [UIColor themeColor];
        [_sendButton addTarget:self action:@selector(option_sendButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendButton;
}


- (LPChooseYearAndMonthAndDayAndHourAndMinuteView *)pickTime{
    if (!_pickTime) {
        _pickTime  = [[LPChooseYearAndMonthAndDayAndHourAndMinuteView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
        _pickTime.delegate = self;
    }
    return _pickTime;
}


@end
